import { generatePortInfoVo } from '@/config/house/generateVo';
import { configHouse } from '@/config/house/houseDetailConfig';
import { protTagOptions } from '@/config/portTag';

const mixinMethods = {
  /** 房源信息格式化 */
  houDelVOFormat(vo) {
    vo.propetyTime = vo.propetyTime || 70;
    vo.houYears = vo.houYears || '满五年';
    vo.onlyHou = vo.onlyHou || 0;
    vo.cookroom = vo.cookroom === 0 ? 1 : vo.cookroom;
    vo.balcony = vo.balcony === 0 ? 1 : vo.balcony;

    for (let key in vo) {
      if (Number(vo[key]) == -1) vo[key] = '';
      if (typeof vo[key] == 'boolean' || vo[key] === '') continue;
      if (vo[key] >= 0) {
        vo[key] = Number(vo[key]);
      }
    }

    configHouse
      .filter((item) => {
        return item.isRadio && item.options && item.options.length > 2;
      })
      .forEach((item) => {
        if (!item.options.includes(vo[item.key])) {
          vo[item.key] = '';
        }
      });

    return vo;
  },
  // 保存房源介绍信息的数据格式化
  getRecommendVo(houDelRecommendVO, houseRecommend) {
    let vo = Object.keys(houDelRecommendVO).reduce((pre, next) => {
      let item = houseRecommend.find((v) => v.key == next);
      if (item) {
        pre[next] = {
          tag: item.title,
          context: houDelRecommendVO[next],
        };
      }
      return pre;
    }, {});
    return vo;
  },

  getTotalPrices(vo, price) {
    price = price || vo.totalPrices;
    if (vo.delegationType == 'RENT') {
      return price > 0 ? price : '';
    } else if (vo.delegationType == 'SALES') {
      return price > 0 ? price / 10000 : '';
    }
  },
  setTotalPrices(vo, price) {
    price = price || vo.totalPrices;
    if (vo.delegationType == 'RENT') {
      return price > 0 ? price : '';
    } else if (vo.delegationType == 'SALES') {
      return price > 0 ? price * 10000 : '';
    }
  },
  numberInput() {
    this.value = this.value.replace(/[^\d.]/g, '');
  },
  isValidValue(v) {
    if (v === 0) return true;
    if (v === '0') return true;
    if (typeof v == 'string' && v.trim() === '') {
      return false;
    }
    return !!v;
  },
  getPortTagValue(key, list) {
    if (!list || list.length == 0) return [];
    let data = protTagOptions.map((v) => v.list);
    data = data.reduce((pre, next) => {
      return pre.concat(next);
    }, []);
    const result = list
      .map((item) => {
        return data.find((v) => v[key] == item);
      })
      .filter((v) => !!v);
    if (!result.length) {
      return [];
    }
    let k = key == 'text' ? 'value' : 'text';
    return result.map((v) => v[k]);
  },

  /** 端口号信息加工 */
  getHouPortInfoFormat(houPortInfo, houDelVO, pushAccounts) {
    const list = pushAccounts.map(({ portType }) => {
      let info = houPortInfo.find((v) => v.portType == portType) || { portType };

      const _size = info.buildingSize || houDelVO.buildSize
      info = generatePortInfoVo({
        ...info,
        delId: houDelVO.id,
        totalPrice: mixinMethods.getTotalPrices(houDelVO, info.totalPrice || houDelVO.totalPrices),
        buildingSize: _size,
        publishSize: info.publishSize || _size
      });

      return info;
    });

    return list;
  },
};

export default mixinMethods;
